using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreData;
using EntityData;
using Helper;
namespace Services
{

    public partial class UsersService
    {
       readonly IUsersEntity entity;
       readonly IStaffEntity Staffentity;
       public UsersService()
       {
           this.entity = new UsersEntity();
           Staffentity = new StaffEntity();

       }

       #region Main Method

       /// <summary>
       /// Save 
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public long Save(object _model)
       {
           try
           {
               var id = long.Parse(_model.GetType().GetProperty("ID").GetValue(_model, null).ToString());
               if (id == 0)
               {
                   return entity.Save((CoreData.Users)_model, Table.Users.ToString());
               }
               else
               {
                   return entity.Update((CoreData.Users)_model, Table.Users.ToString());
               }
           }
           catch { return -1; }
       }

       /// <summary>
       /// Get by ID
       /// </summary>
       /// <param name="_id"></param>
       /// <returns></returns>
       public CoreData.Users GetByID(long _id)
       {
           try
           {
               return entity.GetById(_id, Table.Users.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Users> GetList()
       {
           try
           {
               return entity.GetAll(Table.Users.ToString()).ToList();
           }
           catch { return null; }

       }

       /// <summary>
       /// Get List with Linq
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Users> GetListByLINQ(Func<CoreData.Users, Boolean> _where)
       {
           try
           {
               return entity.GetMany(_where, Table.Users.ToString()).ToList();
           }
           catch { return null; }
       }
       /// <summary>
       /// Get One with Linq
       /// </summary>
       /// <returns></returns>
       public CoreData.Users GetOneByLINQ(Func<CoreData.Users, Boolean> _where)
       {
           try
           {
               return entity.Get(_where, Table.Users.ToString());
           }
           catch { return null; }
       }

       /// <summary>
       /// Get List with search string
       /// </summary>
       /// <returns></returns>
       public IList<CoreData.Users> GetList(string _searchstring)
       {
           //search theo tieu chi nao do         
           try
           {
               return entity.GetBySearchString(_searchstring, Table.Users.ToString()).ToList();
           }
           catch { return null; }
       }


       /// <summary>
       /// Delete
       /// </summary>
       /// <param name="_model"></param>
       /// <returns></returns>
       public bool Delete(object _model)
       {
           try
           {
               entity.Delete((CoreData.Users)_model, Table.Users.ToString());
               return true;
           }
           catch { return false; }
       }


       #endregion



        #region Other Method

        /// <summary>
        /// Get User Module role
        /// </summary>
        /// <param name="Username"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
       public IList<CoreData.Permission> GetUserModuleRole(string Username, string Password, ref string mess, ref long userid, ref long staffid, ref string staffname)
       {
           try
           {
               var data = entity.GetByCusTomSQL(String.Format(SQLCommand.GetUserModuleRole, Username, Password)).ToList();
               IList<CoreData.Permission> P = new List<CoreData.Permission>();
               var checkvaliddate = data.First();
               userid = checkvaliddate.ID;
               var staff = Staffentity.GetById(checkvaliddate.StaffID, Table.Staff.ToString());
               staffname = staff.Name;
               staffid = staff.ID;
               if(checkvaliddate.Endate<=DateTime.Now)
               {
                  if(Helper.ValueDefine.Lang == ValueDefine.Language.VN)
                  {
                      mess = Helper.ErrorCodeVN.ExpriedAccount;
                  }
                  else
                  {
                      mess = Helper.ErrorCodeEN.ExpriedAccount;
                  }
                  return null;
                }
               foreach(var item in data)
               {
                   CoreData.Permission p = new CoreData.Permission();
                   p.UserName = item.UserName;
                   p.Module = item.ModuleExt;
                   p.Role = item.RoleExt;
                   P.Add(p);
               }
               return P;
           }
           catch { return new List<CoreData.Permission>(); }
       }
      
        /// <summary>
        /// Get Group module role
        /// </summary>
        /// <param name="Username"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public  IList<CoreData.Permission> GetGroupModuleRole(string Username, string Password, ref string mess, ref long userid, ref long staffid, ref string staffname)
        {
            var data = entity.GetByCusTomSQL(String.Format(SQLCommand.GetGroupModuleRole, Username, Password)).ToList();
            IList<CoreData.Permission> P = new List<CoreData.Permission>();
            if (data == null || data.Count <= 0)
                return null;
            var checkvaliddate = data.First();
            userid = checkvaliddate.ID;
            var staff = Staffentity.GetById(checkvaliddate.StaffID, Table.Staff.ToString());
            staffname = staff.Name;
            staffid = staff.ID;
            if (checkvaliddate.Endate <= DateTime.Now)
            {
                if (Helper.ValueDefine.Lang == ValueDefine.Language.VN)
                {
                    mess = Helper.ErrorCodeVN.ExpriedAccount;
                }
                else
                {
                    mess = Helper.ErrorCodeEN.ExpriedAccount;
                }
                return null;
            }
            foreach (var item in data)
            {
                CoreData.Permission p = new CoreData.Permission();
                p.UserName = item.UserName;
                p.Module = item.ModuleExt;
                p.Role = item.RoleExt;
                P.Add(p);
            }
            return P;
        }

        #endregion 
    
    }
         
}
